package org.tgcloud.group.web.controller.app;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.service.app.MediaService;
import org.tgcloud.group.web.controller.dto.media.MediaExportMsgDTO;
import org.tgcloud.group.web.controller.req.media.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: zhy
 * @Created: 2025-02-13-下午1:41
 * @Description:
 */
@Api(tags = "媒体资源app相关接口")
@RestController
@RequestMapping("/app")
@EnableAutoConfiguration
public class MediaController extends ClientAPPBaseController{

    @Autowired
    private MediaService mediaService;

    @ApiOperation(value = "媒体资源列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getMediaAppList")
    public APPResultMessage getMediaAppList(@RequestBody MediaReq req) {
        return mediaService.getMediaAppList(req);
    }

    @ApiOperation(value = "媒体资源分类列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getMediaTypeAppList")
    public APPResultMessage getMediaTypeAppList() {
        return mediaService.getMediaTypeAppList();
    }



    @ApiOperation(value = "媒体资源详情")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getMediaAppMsgList")
    public APPResultMessage getMediaAppMsgList(@RequestBody MediaIdsReq req) {
        return mediaService.getMediaAppMsgList(req);
    }


    @ApiOperation(value = "媒体资源范围内列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getMediaAppRadiusList")
    public APPResultMessage getMediaAppRadiusList(@RequestBody MediaRadiusPageReq req) {
        return mediaService.getMediaAppRadiusList(req);
    }


    @ApiOperation(value = "计算媒体资源范围内的合计")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getMediaAppRadiusCount")
    public APPResultMessage getMediaAppRadiusCount(@RequestBody MediaRadiusCountReq req) {
        return mediaService.getMediaAppRadiusCount(req);
    }


    @ApiOperation(value = "获取媒体资源范围内的所有筛选条件")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getMediaAppRadiusSearchList")
    public APPResultMessage getMediaAppRadiusSearchList(@RequestBody MediaRadiusCountReq req) {
        return mediaService.getMediaAppRadiusSearchList(req);
    }

    @ApiOperation(value = "导出媒体资源")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody MediaRadiusPageReq req) throws IOException {
        // 准备要导出的数据
        List<MediaExportMsgDTO> data = mediaService.prepareUserData(req);
        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("export", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 使用EasyExcel写出数据到输出流
        try {
            // 设置单元格样式
//            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//            WriteFont headWriteFont = new WriteFont();
//            headWriteFont.setBold(true);
//            headWriteCellStyle.setWriteFont(headWriteFont);

            EasyExcel.write(response.getOutputStream(), MediaExportMsgDTO.class)
                    .autoCloseStream(Boolean.FALSE)
//                    .registerWriteHandler(new CustomSheetWriteHandler()) // 注册自定义处理器来设置样式
                    .sheet("Sheet1")
                    .doWrite(data);
        } catch (Exception e) {
            e.printStackTrace();
            response.reset(); // 发生异常时重置响应对象
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println("{\"code\":500,\"msg\":\"导出失败\"}");
        }
    }


}
